package com.nep;

import com.nep.common.util.ApplicationContextUtil;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import java.io.IOException;
import java.net.URL;
import javafx.application.Platform;


public class Launch extends Application {

    @Override
    public void start(Stage primaryStage) {

        ApplicationContextUtil.getInstance().setPrimaryStage(primaryStage);

        // 启动画面
        Stage splashStage = new Stage(StageStyle.UNDECORATED);
        var imageUrl = getClass().getResource("/com/nep/image/welcome.jpg");
        if (imageUrl == null) {
            throw new RuntimeException("欢迎图片未找到");
        }
        ImageView splashImage = new ImageView(new Image(imageUrl.toString()));
        splashImage.setPreserveRatio(true);
        splashImage.setFitWidth(600);

        StackPane splashPane = new StackPane(splashImage);
        Scene splashScene = new Scene(splashPane);

        splashStage.setScene(splashScene);
        splashStage.show();

        // 跳转至主菜单
        new Thread(() -> {
            try {
                Thread.sleep(100);
                Platform.runLater(() -> {
                    Parent root = null;
                    URL fxmlUrl = getClass().getResource("/com/nep/mainMenu/view/MainMenuView.fxml");
                    if (fxmlUrl == null) {
                        throw new RuntimeException("登录界面FXML文件未找到");
                    }
                    try {
                        FXMLLoader loader = new FXMLLoader(fxmlUrl);
                        root = loader.load();

                    } catch (IOException e) {
                        throw new RuntimeException("加载登录界面FXML文件失败", e);
                    }

                    Scene mainScene = new Scene(root);
                    primaryStage.setScene(mainScene);
                    primaryStage.setTitle("NEPS - 公众监督员登录");
                    primaryStage.show();
                    splashStage.close();
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}